/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { Scene } from 'arkanalyzer/lib/Scene';
import { SceneConfig, Sdk } from 'arkanalyzer/lib/Config';
import Logger, {LOG_LEVEL, LOG_MODULE_TYPE} from 'arkanalyzer/lib/utils/logger';
import { FileUtils, WriteFileMode } from '../../../src/tools/utils/FileUtils';
import { buildModuleDepGraph } from '../../../src/tools/builder';
import { Config } from '../../../src/tools/Config';

const logPath = 'out/ArkAnalyzer.log';
const logger = Logger.getLogger(LOG_MODULE_TYPE.TOOL, 'moduleDepTest');
Logger.configure(logPath, LOG_LEVEL.ERROR, LOG_LEVEL.ERROR);

// 加载配置
const config = Config.getInstance();
config.loadFromFile('test/resources/config/config4ModuleDep.json'); // 从配置文件加载

const projectDir = config.getProjectPath();
const sceneConfig = new SceneConfig();
let sdk: Sdk = config.getSdk();
sceneConfig.getSdksObj().push(sdk);

sceneConfig.buildFromProjectDir(projectDir);

const scene = new Scene();
scene.buildSceneFromFiles(sceneConfig);
scene.inferTypes();

const moduleDepGraphJson = config.getOutputFilePath('moduleDepGraph.json');
const moduleDepGraphDot = config.getOutputFilePath('moduleDepGraph.dot');

logger.info('Started to build module dependency graph...');

const depGraph = buildModuleDepGraph(scene);

const jsonRes = JSON.stringify(depGraph.toJson());
FileUtils.writeToFile(moduleDepGraphJson, jsonRes, WriteFileMode.OVERWRITE);
logger.info('Building module graph in json format has completed.');

const dotRes = depGraph.dump();
FileUtils.writeToFile(moduleDepGraphDot, dotRes, WriteFileMode.OVERWRITE);
logger.info('Building module graph in dot format has completed.');

logger.info('Building file module graph completed.');